home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / parsingcommandline / example1.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  8KB  |  202 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Parsing Command Line        Tulevagen 22       */
  8. /* File:    Example1.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-06                                       */
  11. /* Version: 1.0                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to parse the command line.  */
  21. /* Since this is the first example it is relative simple.    */
  22. /* The program expects one argument. If no argument is given */
  23. /* will the parse function fail (uses the "/A" - "Always     */
  24. /* required" option), and if more than one argument is give  */
  25. /* it will also fail (the "/M" - "Multiple argument" option  */
  26. /* is not set).                                              */
  27.  
  28.  
  29.  
  30. /* Include the dos library definitions: */
  31. #include <dos/dos.h>
  32.  
  33. /* Include information about the argument parsing routine: */
  34. #include <dos/rdargs.h>
  35.  
  36. /* Now we include the necessary function prototype files:         */
  37. #include <clib/dos_protos.h>       /* General dos functions...    */
  38. #include <clib/exec_protos.h>      /* System functions...         */
  39. #include <stdio.h>                 /* Std functions [printf()...] */
  40. #include <stdlib.h>                /* Std functions [exit()...]   */
  41.  
  42.  
  43.  
  44. /* Here is our simple command line template. This program expects */
  45. /* one argument. If no argument is given will the parse function  */
  46. /* fail since we have set the option "/A" ("Always required"),    */
  47. /* and if more than one argument is given it will also fail since */
  48. /* we have not set the "/M" ("Multiple argument") option.         */
  49. #define MY_COMMAND_LINE_TEMPLATE "SoundFiles/A"
  50.  
  51. /* Here is a valid command line:                                  */
  52. /*   Example1 Bird.snd                                            */
  53. /*                                                                */
  54. /* Here are some incorrect command lines:                         */
  55. /*   Example1                      The file name is required!     */
  56. /*   Example1 Bird.snd River.snd   Only one argument may be used! */
  57.  
  58.  
  59.  
  60. /* Only one command template is used: */
  61. #define NUMBER_COMMAND_TEMPLATES 1
  62.  
  63. /* The command template numbers: (Where the result of each */
  64. /* command template can be found in the "arg_array".)      */
  65. #define SOUNDFILE_TEMPLATE 0
  66.  
  67.  
  68.  
  69. /* Set name and version number: */
  70. UBYTE *version = "$VER: AmigaDOS/ParsingCommandLine/Example1 1.0";
  71.  
  72.  
  73.  
  74. /* Declare an external global library pointer to the Dos library: */
  75. extern struct DosLibrary *DOSBase;
  76.  
  77.  
  78.  
  79. /* Declared our own function(s): */
  80.  
  81. /* Our main function: */
  82. int main( int argc, char *argv[] );
  83.  
  84.  
  85.  
  86. /* Main function: */
  87.  
  88. int main( int argc, char *argv[] )
  89. {
  90.   /* Simple loop variable: */
  91.   int loop;
  92.  
  93.   /* Pointer to a RDArgs structure which will automatically */
  94.   /* be created for us when we use the RDArgs() function:   */
  95.   struct RDArgs *my_rdargs;
  96.  
  97.   /* The ReadArgs() function needs an arrya of LONGs where */
  98.   /* the result of the command parsing will be placed. One */
  99.   /* LONG variable is needed for every command template.   */
  100.   LONG arg_array[ NUMBER_COMMAND_TEMPLATES ];
  101.  
  102.   /* Note! This "arg_array" must be cleared (all values set to */
  103.   /* zero) before we may use it with the ReadArgs() function.  */
  104.   /* If we declare this structure outside the main function    */
  105.   /* all values will automatically be cleared by C, but if we, */
  106.   /* as in this example, declare the array inside a function   */
  107.   /* we have to clear it manually. (If we do not clear it we   */
  108.   /* can not examine the array and see if a field is set or    */
  109.   /* not.)                                                     */
  110.  
  111.  
  112.  
  113.   /* The built in command parsing routine was first  */
  114.     /* introduced in Release 2. V36 of the dos library */
  115.     /* was however rather "buggy", and you should only */
  116.     /* use V37 or higher:                              */
  117.   if( DOSBase->dl_lib.lib_Version < 37 )
  118.   {
  119.     /* Too old dos library! */
  120.     printf( "This program needs Dos Library V37 or higher!\n" );
  121.    
  122.     /* Exit with an error code: */ 
  123.     exit( 20 );
  124.   }
  125.  
  126.  
  127.  
  128.   /* We will now clear the "arg_array" (set all values to zero): */
  129.   for( loop = 0; loop < NUMBER_COMMAND_TEMPLATES; loop++ )
  130.     arg_array[ loop ] = 0;
  131.  
  132.  
  133.  
  134.   /* Parse the command line: (ReadArgs() will read the command */
  135.   /* line and with the help of the command line template set   */
  136.   /* the corresponding values in the "arg_array" which is used */
  137.   /* to store the result of the command parsing. The function  */
  138.   /* will return a pointer to a RDArgs structure which has     */
  139.   /* automatically been created for us, since we did not create  */
  140.   /* one ourself. This structure must be removed with help of    */
  141.   /* the FreeArgs() function before your program may terminate.) */
  142.   my_rdargs = 
  143.     ReadArgs( MY_COMMAND_LINE_TEMPLATE,
  144.               arg_array,
  145.               NULL
  146.             );
  147.  
  148.   /* Have AmigaDOS successfully parsed our command line? */
  149.   if( !my_rdargs )
  150.   {
  151.     /* The command line could not be parsed! The user probably */
  152.     /* forgot to enter an argument which is required.          */
  153.     printf( "Could not parse the command line!\n" );
  154.  
  155.     /* See you later... */
  156.     exit( 21 );
  157.   }
  158.  
  159.  
  160.  
  161.   /* The comand line has successfully been parsed! */
  162.   /* We can now examine the "arg_array":           */
  163.  
  164.   /* Print template 1, the file name, which is in this example the */
  165.   /* only argument. Since the user must enter this argument, or    */
  166.   /* else the ReadArgs() function would fail, we actually do not   */
  167.   /* have to check that there is something in the array. However,  */
  168.   /* it is easy to remove the "/A" option and forget to add a      */
  169.   /* check later on, so we better always check that there is       */
  170.   /* something in the array before we use it (you can't be too     */
  171.   /* careful).                                                     */
  172.   /*                                                               */
  173.   /* (If the user would not have entered an argument and the "/A"  */
  174.   /* option was not used, the LONG variable in the array would be  */
  175.   /* NULL. If the user has entered an argument, which he must have */
  176.   /* done in this example since it is required, the LONG variable  */
  177.   /* in the arraw contains a pointer to a string with the argument */
  178.   /* inside.)                                                      */
  179.   if( arg_array[ SOUNDFILE_TEMPLATE ] )
  180.     printf( "File name: %s\n", arg_array[ SOUNDFILE_TEMPLATE ] );
  181.  
  182.  
  183.  
  184.   /* Before our program terminates we have to free the data that */
  185.   /* have been allocated when we successfully called ReadArgs(): */
  186.   FreeArgs( my_rdargs );
  187.  
  188.   /* Please note that any pointers in the "arg_array" which   */
  189.   /* pointed to some data, for example strings, may not be    */
  190.   /* used any more after you have called FreeArgs(). The data */
  191.   /* (strings etc...) have now been deallocated, and can not  */
  192.   /* be accessed any more.                                    */
  193.   
  194.  
  195.  
  196.   /* "And they lived happily ever after..." */
  197.   exit( 0 );
  198. }
  199.  
  200.  
  201.  
  202.